<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\Refund;
use App\Admin\Actions\Grid\Topup;
use App\Admin\Actions\Grid\Consume;
use App\Admin\Repositories\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(['balance']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('sn')->editable();
            $grid->column('name');
            $grid->column('gender')->display(function () {
                return $this->gender == 1 ? '男' : '女';
            });
            $grid->column('balance.balance', '余额')->display(function () {
                return $this->balance->balance ?? 0;
            });
            $grid->column('phone')->editable();
            $grid->column('birthday')->editable();
            $grid->column('pdf')->display(function () {
                $txt = '-';
                if (!empty($this->pdf)) {
                    $txt = "<a target='_blank' href='". url('uploads/' . $this->pdf) ."'>查看</a>";
                }
                return $txt;
            });

            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->select('user.balance', '筛选',
                    ['未充值', '已充值', '已退款'], function ($query, $value) {

                    $array = ['未充值', '已充值', '已退款'];
                    $value = current($value);
                    $current = $array[$value];

                    if ($current == '已充值') {
                        $query->has('balanceFlow');
                    } elseif ($current == '未充值') {
                        $query->doesnthave('balanceFlow');
                    } elseif ($current == '已退款') {
                        $query->has('refund');
                    }

                });
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->like('sn')->width(4);
                $filter->like('name')->width(4);
                $filter->like('phone')->width(4);
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $id = $this->id;

                // 充值
                $topup = new Topup();
                $actions->append($topup);

                // 增加消费
                $consume = new Consume();
                $actions->append($consume);

                // 退款
                $refund = new Refund();
                $actions->append($refund);

                // 消费流水
                $urlFlow = 'user_balance_flow?user_id=' . $id;
                $btn = "<a target='_blank' href='$urlFlow'>消费记录</a>";
                $actions->append($btn);

                $url = 'user_fitness?user_id=' . $id;
                $btn = '<a target="_blank" href="'. $url .'"  class="">体态评估表</a>';
                $actions->append($btn);

                $urlHealth = 'user_health?user_id=' . $id;
                $btnHealth = "<a target='_blank' href='$urlHealth' class=''>健康档案管理</a>";
                $actions->append($btnHealth);

                $urlHealth2 = 'user_health_2?user_id=' . $id;
                $btnHealth2 = "<a target='_blank' href='$urlHealth2' class=''>健康档案管理2</a>";
                $actions->append($btnHealth2);

                $actions->disableDelete();
                $actions->disableView();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->field('id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->column(6, function (Form $form) {
                $form->display('id');
                $form->text('name');
                $form->text('sn');
                $form->select('gender')->options([0 => '女', 1 => '男']);
                $form->text('phone');
                $form->date('birthday');
                $form->textarea('address');
            });

            $form->column(6, function (Form $form) {
                $pdf = $form->model()->pdf;
                if (!empty($pdf)) {
                    $helpText = "<a target='_blank' href='". url('uploads/' . $pdf) ."'>查看</a>";
                } else {
                    $helpText = '未上传档案';
                }
                $form->image('image')->autoUpload();
                $form->file('pdf')->autoUpload()->help($helpText);
                $form->textarea('comment');
            });


            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
